package com.hrzyit.whistle.cases.widgets

import android.content.Context
import android.view.View
import android.widget.AdapterView
import android.widget.SimpleAdapter
import android.widget.Toast
import com.hrzyit.base.ext.onClick
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.data.bean.CaseInfoEntity
import com.hrzyit.whistle.cases.data.bean.CaseSignInVo
import com.hrzyit.whistle.cases.presenter.CaseInfoPresenter
import com.hrzyit.whistle.cases.ui.adapter.RejectReasonAdapter
import com.lxj.xpopup.animator.PopupAnimator
import com.lxj.xpopup.core.CenterPopupView
import kotlinx.android.synthetic.main.popup_case_accept.view.*
import org.jetbrains.anko.toast

/**
 * 弹出窗口：案件不受理(指挥中心到单位)界面
 * @author YangWei
 * @date 2019/4/21
 */
class CaseAcceptPopup (context: Context,val presenter: CaseInfoPresenter): CenterPopupView(context) {

    lateinit var caseSignInVo: CaseSignInVo

    // 返回自定义弹窗的布局
    override fun getImplLayoutId(): Int {
        return R.layout.popup_case_accept
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    override fun onCreate() {
        super.onCreate()
        // 获取数据不受理原因
        caseSignInVo.let{
            val desc = if (it.reasonDesc.isNullOrBlank()) {""} else{it.reasonDesc}
            mReasonDescEt.setText("$desc")
        }

        // Init spinner for reject reason
        val rejectReasonAdapter= SimpleAdapter(
            context, buildRejectReasonList(),
            R.layout.support_simple_spinner_dropdown_item,
            arrayOf("text"), intArrayOf(android.R.id.text1)
        )
        mRejectReasonSpinner.adapter = rejectReasonAdapter

        mRejectReasonSpinner.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val item = parent!!.getItemAtPosition(position) as MutableMap<String, Long>
                caseSignInVo.reasonId = item["id"]!!
            }

        }

        mConfirmBtn.onClick{
            //
//            context.toast("${mReasonDescEt?.text}")
            mReasonDescEt?.let {
                val info = if (it.text.isNotBlank()) {it.text} else {""}
                caseSignInVo.reasonDesc = "$info"
            }
            if (caseSignInVo.reasonId > 0L) {
                presenter.signInCaseFromCenter(caseSignInVo)
            } else {
                context.toast("请选择不受理原因")
            }

        }
        mCancelBtn.onClick{
            dismiss() // 关闭弹窗
        }
    }

    /**
     * 组织不受理原因选择数据
     */
    private fun buildRejectReasonList(): MutableList<out MutableMap<String, *>>? {

        val dataList = mutableListOf<MutableMap<String,*>>()

        val item = mutableMapOf<String,Any>()
        val item2 = mutableMapOf<String,Any>()
        val item3 = mutableMapOf<String,Any>()
        val item4 = mutableMapOf<String,Any>()
        val item5 = mutableMapOf<String,Any>()
        val item6 = mutableMapOf<String,Any>()
        val item7 = mutableMapOf<String,Any>()
        val item8 = mutableMapOf<String,Any>()
        val item9 = mutableMapOf<String,Any>()
        val item10 = mutableMapOf<String,Any>()
        val item11 = mutableMapOf<String,Any>()
        val item12 = mutableMapOf<String,Any>()
        val item13 = mutableMapOf<String,Any>()
        val item14 = mutableMapOf<String,Any>()
        val item15 = mutableMapOf<String,Any>()
        item["id"] = 78
        item2["id"] = 79
        item3["id"] = 80
        item4["id"] = 81
        item5["id"] = 82
        item6["id"] = 83
        item7["id"] = 84
        item8["id"] = 85
        item9["id"] = 86
        item10["id"] = 87
        item11["id"] = 88
        item12["id"] = 89
        item13["id"] = 90
        item14["id"] = 91
        item15["id"] = 92

        item["text"] = "照片不清或照片不符"
        item2["text"] = "流动性强"
        item3["text"] = "骚扰电话"
        item4["text"] = "咨询"
        item5["text"] = "其他"
        item6["text"] = "大小类不符"
        item7["text"] = "不符合立案标准"
        item8["text"] = "监督员信息不准确(照片)"
        item9["text"] = "监督员信息不准确(录音)"
        item10["text"] = "自行处理"
        item11["text"] = "监督员上报信息不准确"
        item12["text"] = "系统原因案件超时"
        item13["text"] = "不必处置"
        item14["text"] = "暂不适合监督员上报"
        item15["text"] = "核查任务按新任务上报"

        dataList.add(item)
        dataList.add(item2)
        dataList.add(item3)
        dataList.add(item4)
        dataList.add(item5)
        dataList.add(item6)
        dataList.add(item7)
        dataList.add(item8)
        dataList.add(item9)
        dataList.add(item10)
        dataList.add(item11)
        dataList.add(item12)
        dataList.add(item13)
        dataList.add(item14)
        dataList.add(item15)

        return dataList
    }

    // 设置最大宽度，看需要而定
    override fun getMaxWidth(): Int {
        return super.getMaxWidth()
    }

    // 设置最大高度，看需要而定
    override fun getMaxHeight(): Int {
        return super.getMaxHeight()
    }

    // 设置自定义动画器，看需要而定
    override fun getPopupAnimator(): PopupAnimator {
        return super.getPopupAnimator()
    }
}